package cn.getword.cnblogs.entity;

import org.apache.commons.lang3.builder.ToStringBuilder;

/**
 * @Description //TODO 分页信息实体类
 * @Author zhuxiang
 * @Date 21:31 2020/4/16
 * @Param
 * @return
 **/
public class Page {
	public final static String ORDER_DIRECTION_ASC = "asc"; //Bruce Xu Modified
	public final static String ORDER_DIRECTION_DESC = "desc";//Bruce Xu Modified
	
	/**
	 * 默认每页记录数
	 */
	private static final int DEFAULT_PAGE_SIZE = 15;
	
	/**
	 * 原始页码
	 */
	private int plainPageNum = 1;
	
	/**
	 * 当前页码
	 */
	private int pageNum = 1;
	/**
	 * 每页记录条数
	 */
	private int numPerPage = DEFAULT_PAGE_SIZE;
	/**
	 * 排序条件
	 */
	private String orderField = "";
	/**
	 * 排序顺序,DESC\ASC
	 */
	private String orderDirection = ORDER_DIRECTION_DESC;
	
	/**
	 * 总页数
	 */
	private int totalPage = 1;

	/**
	 * 前一页
	 */
	private int prePage = 1;

	/**
	 * 下一页
	 */
	private int nextPage = 1;
	
	/**
	 * 总记录数
	 */
	private long totalCount = 0;
	
	/**
	 * Bruce Xu Add
	 * 当前页记录数起始位置(FOR MYBATIS)
	 */
	private int pageBeginCount;

	/**  
	 * 返回 pageNum(当前页码) 的值   
	 * @return pageNum  
	 */
	public int getPageNum() {
		if (pageNum > totalPage) {
			pageNum = totalPage;
		}
		return pageNum;
	}

	/**  
	 * 设置 pageNum(当前页码) 的值  
	 * @param pageNum
	 */
	public void setPageNum(int pageNum) {
		this.pageNum = pageNum > 0 ? pageNum : 1;
		this.plainPageNum = this.pageNum;
	}

	/**  
	 * 返回 numPerPage(每页记录条数) 的值   
	 * @return numPerPage  
	 */
	public int getNumPerPage() {
		return numPerPage;
	}

	/**  
	 * 设置 numPerPage(每页记录条数) 的值  
	 * @param numPerPage
	 */
	public void setNumPerPage(int numPerPage) {
		this.numPerPage = numPerPage > 0 ? numPerPage : 10;
	}

	/**  
	 * 返回 orderField(排序条件) 的值   
	 * @return orderField  
	 */
	public String getOrderField() {
		return orderField;
	}

	/**  
	 * 设置 orderField(排序条件) 的值  
	 * @param orderField
	 */
	public void setOrderField(String orderField) {
		this.orderField = orderField;
	}

	/**  
	 * 返回 orderDirection(排序顺序) 的值   
	 * @return orderDirection  
	 */
	public String getOrderDirection() {
		return orderDirection;
	}

	/**  
	 * 设置 orderDirection(排序顺序) 的值  
	 * @param orderDirection
	 */
	public void setOrderDirection(String orderDirection) {
		this.orderDirection = orderDirection;
	}

	/**  
	 * 返回 totalPage(总页数) 的值   
	 * @return totalPage  
	 */
	public int getTotalPage() {
		return totalPage;
	}

	/**  
	 * 设置 totalPage(总页数) 的值  
	 * @param totalPage
	 */
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	/**  
	 * 返回 prePage(前一页) 的值   
	 * @return prePage  
	 */
	public int getPrePage() {
		prePage = pageNum - 1;
		if (prePage < 1) {
			prePage = 1;
		}
		return prePage;
	}

	/**  
	 * 设置 prePage(前一页) 的值  
	 * @param prePage
	 */
	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	/**  
	 * 返回 nextPage(后一页) 的值   
	 * @return nextPage  
	 */
	public int getNextPage() {
		nextPage = pageNum + 1;
		if (nextPage > totalPage) {
			nextPage = totalPage;
		}
		
		return nextPage;
	}

	/**  
	 * 设置 nextPage(后一页) 的值  
	 * @param nextPage
	 */
	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	/**  
	 * 返回 totalCount(记录总条数) 的值   
	 * @return totalCount  
	 */
	public long getTotalCount() {
		return totalCount;
	}

	/**  
	 * 设置 totalCount(记录总条数) 的值  
	 * @param totalCount
	 */
	public void setTotalCount(long totalCount) {
		this.totalCount = totalCount;
		totalPage = (int)(totalCount - 1) / this.numPerPage + 1;
	}

	/**  
	 * 返回 plainPageNum(原始页码) 的值   
	 * @return plainPageNum  
	 */
	public int getPlainPageNum() {
		return plainPageNum;
	}

	/**  
	 * 设置 plainPageNum(原始页码) 的值  
	 * @param plainPageNum
	 */
	public void setPlainPageNum(int plainPageNum) {
		this.plainPageNum = plainPageNum;
	}
	/**
	 * Bruce Xu Add
	 * FOR MYBATIS 
	 * 获取当前页数据库记录偏移位置
	 * @param pageIndex
	 * @param pageCount
	 * @return
	 */
	public int getPageBeginCount() {
		pageBeginCount=(this.plainPageNum-1)*this.numPerPage;
		return pageBeginCount;
	}
	/**
	 * Bruce Xu Add
	 * FOR MYBATIS
	 * 设置当前页数据库记录偏移位置
	 * @param pageBeginCount
	 */
	public void setPageBeginCount(int pageBeginCount) {
		this.pageBeginCount = pageBeginCount;
	}
	
	/**
	 * reflectionToString将对象输出为string类型。
	 */
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this);
	}
}
